package com.miaowang.salad.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.Common;
import com.miaowang.salad.adapter.MyServiceAdapter;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.data.ServiceListData;
import com.miaowang.salad.jobqueue.GetHistoryServiceListJob;
import com.miaowang.salad.jobqueue.JobEvents;
import com.miaowang.salad.util.CheckUtil;
import com.path.android.jobqueue.Job;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MyServiceActivity extends BaseActivity {

	ListView lvService;
	MyServiceAdapter myServiceAdapter;
	List<ServiceListData> serviceDataList;
	private ProgressDialog pd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_service_list);
		initComponent();
		initData();
		
	}

	private void getHistoryList() {
		if (pd == null) {
			pd = new ProgressDialog(MyServiceActivity.this);
		}
		pd.setTitle("正在获取历史任务信息...");
		pd.show();
		Job job = new GetHistoryServiceListJob();
		CarmapApplication.getInstance().getJobManager().addJobInBackground(job);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		getHistoryList();
	}

	private void initComponent() {
		lvService = (ListView) findViewById(R.id.lv_service);
		serviceDataList = new ArrayList<ServiceListData>();
//		for (int i = 0; i < 16; i++) {
//			ServiceListData serviceListData = new ServiceListData();
//			serviceListData.setId(String.valueOf(i));
//			serviceListData.setService_type("更换轮胎");
//			serviceListData.setStatus("已创建");
//			serviceListData.setCreated_at("2015-08-26 01:55");
//			serviceDataList.add(serviceListData);
//		}
		myServiceAdapter = new MyServiceAdapter(MyServiceActivity.this,
				serviceDataList);
		lvService.setAdapter(myServiceAdapter);
	}

	private void initData() {
		lvService.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("id", serviceDataList.get(position).getId());
				intent.putExtra("type", "1");
				intent.setClass(MyServiceActivity.this,
						ServiceDetailActivity.class);
				MyServiceActivity.this.startActivity(intent);
			}
		});
	}

	// EventsBus
	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobSuccess e) {
		if (e.getJobCode() == JobEvents.GET_SERVICE_REQUESTS) {
			if (pd != null)
				pd.dismiss();
            OkHttpResponseData msg = (OkHttpResponseData) e.getResult();
            if(msg != null){
				Toast.makeText(MyServiceActivity.this, msg.getMsg(),
						Toast.LENGTH_SHORT).show();
				if (!CheckUtil.isEmpty(msg.getData())) {
					try {
						Log.i(Common.TAG, msg.getData()+" servicelist data");
						JSONObject json = new JSONObject(msg.getData());
						if (json.has("service_requests")) {
							String jobString = json.getString("service_requests");
							if (!CheckUtil.isEmpty(jobString)) {
								JSONArray jsonArray = new JSONArray(jobString);
								if(serviceDataList != null){
									serviceDataList.clear();
								}else{
									serviceDataList = new ArrayList<ServiceListData>();
								}
								for(int i = 0 ;i < jsonArray.length() ; i++){
									JSONObject jsonObj = jsonArray.getJSONObject(i);
									ServiceListData serviceListData = JSON.parseObject(jsonObj.toString(), ServiceListData.class);
									serviceDataList.add(serviceListData);
								}
								myServiceAdapter.notifyDataSetChanged();
							} else {
								Toast.makeText(MyServiceActivity.this,
										"获取历史任务失败", Toast.LENGTH_SHORT)
										.show();
							}
						} else {
							Toast.makeText(MyServiceActivity.this,
									"获取历史任务失败", Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e1) {
						e1.printStackTrace();
					}

				} else {
					Toast.makeText(MyServiceActivity.this, msg.getMsg(),
							Toast.LENGTH_SHORT).show();
				}

			}else{
            	
            }
		}
	}

	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobFailure e) {
		if (e.getJobCode() == JobEvents.GET_SERVICE_REQUESTS) {
			if (pd != null)
				pd.dismiss();
		}
	}
}
